package com.rescue.controller.admin;

import com.rescue.constant.JwtClaimsConstant;
import com.rescue.forum.dto.AdminLoginDTO;
import com.rescue.forum.vo.AdminLoginVO;
import com.rescue.forum.vo.SysSettingVO;
import com.rescue.properties.JwtProperties;
import com.rescue.result.Result;
import com.rescue.service.SysSettingService;
import com.rescue.service.UserInfoService;
import com.rescue.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/admin")
@RestController
@Api(tags = "管理员登录相关接口")
@Slf4j
public class LoginController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private SysSettingService sysSettingService;

    /**
     * 管理员登录接口
     *
     * @param adminLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("管理员登录接口")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO) {
        log.info("管理员登录接口");
        AdminLoginVO adminLoginVO = userInfoService.login(adminLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, adminLoginVO.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        adminLoginVO.setToken(token);
        return Result.success(adminLoginVO);
    }

    /**
     * 获取系统设置
     * @return
     */
    @ApiOperation("获取系统设置")
    @GetMapping("/forum/setting/getSetting")
    private Result<SysSettingVO> getSysSetting() {
        log.info("获取系统设置");
        SysSettingVO sysSettingVO = sysSettingService.getVO();
        return Result.success(sysSettingVO);
    }

    /**
     * 保存系统设置
     * @param sysSettingVO
     * @return
     */
    @ApiOperation("保存系统设置")
    @PutMapping("/forum/setting/saveSetting")
    private Result saveSetting(@RequestBody SysSettingVO sysSettingVO){
        log.info("保存系统设置");
        sysSettingService.update(sysSettingVO);
        return Result.success();
    }
}
